<?php
class SocialOrigin_5 extends IOSocial{
	protected $app_id 			= 7595795;
	protected $api_key 			= "test";
	protected $api_secret_key 	= "test";
	protected $api_link			= "http://api.vgorode.ru/api.php";
	protected $viewer_id;

	public function __construct($api = Array()){
		$this->settings = $api;
		$this->viewer_id 	= $api["viewer_id"];
		if($this->viewer_id = null){
			$this->secure = 1;
		}
		$this->settings 	= $api;
		$this->hostData 	= $this->extractHostData($this->api_link);
	}
	
	public function checkAuthorization(){
		if($this->settings["auth_key"] = md5($this->app_id . '_' . $this->settings["viewer_id"] . '_' . $this->api_secret_key)){
			return true;
		}else{
			return false;
		}

	}
	
	public function getFriends( $uid ){
		$this->secure = 1;
		$this->request["uid"] = $uid;
		$this->request["fields"] = "uid,first_name,last_name,nickname,sex,bdate,photo,photo_medium,photo_big,online";
		$this->method = "friends.get";
		$req = $this->getApiRequest( $this->expire_time );
		$result = array();
		foreach($req as $value){
			$result[$value["uid"]] = $value;
		}



		return $result;

	}
	
	
	private function genSignature($request_params){
		$signature = "";
	    foreach ($request_params as $key => $value) {
	    	$signature .= $key."=".$request_params[$key];
	    }
		if($this->secure == 1){
			$signature .= $this->api_key;
		}else{
			$signature = $this->viewer_id. $signature. $this->api_secret_key;
		}
		echo "$signature";
       return md5($signature);
	}
	
	protected function getApiRequest( $expir = 0){
	# Настройки соединения.
		$request_params=Array();
		if(count($this->request)>0){
		    foreach($this->request as $key => $value){
		    	$request_params[$key]=$value;
		    }
	    }

	    $request_params['app_id'] = $this->app_id;
	    $request_params['format'] = $this->api_format;
	    $request_params['method'] = $this->method;
	    if($this->secure == 1){
			$request_params['random'] = rand(100000,1000000);
			$request_params['timestamp']=time();

		}
	    ksort($request_params);
		$request_params["sig"] = $this->genSignature($request_params);
	    $mcKey = $this->generateMCKey($request_params);
	    # Пытаемся получить с запроса
		$result = DB::getMCH()->get($mcKey);
		if($result === false or $expir == 0){
			$comon=new HtmlSource();
		    # Формируем тело запроса
		    foreach ($request_params as $key => $value){
	    		$comon->addGetVar($key,$value);
		    }
		    $result = file_get_contents("http://api.vgorode.ru/api.php?".join($comon->getvars,"&"));
		    if($expir > 0){
	   			db::getMCH()->set( $mcKey, $result, 0, $expir);
			}
			unset($comon->postvars);
			
		}
	   	$result=json_decode($result);
	   	$this->request = Array();
	   	$this->method = null;
	    return $this->_objectToArray($result->response);

	}
	
}
?>
